program define statecode

g statecode = .
tempvar statecleaned
g `statecleaned' = upper(`1')
replace `statecleaned' = subinstr(`statecleaned',".","",.)
replace `statecleaned' = subinstr(`statecleaned'," ","",.)

if("`2'" == "postal") {
	di "Using postal abbreviations."
	replace	statecode = 	1 	if(regexm(`statecleaned',"AL"))
	replace	statecode = 	2 	if(regexm(`statecleaned',"AK"))	
	replace	statecode = 	3 	if(regexm(`statecleaned',"AZ"))	
	replace	statecode = 	4 	if(regexm(`statecleaned',"AR"))	
	replace	statecode = 	5 	if(regexm(`statecleaned',"CA"))	
	replace	statecode = 	6 	if(regexm(`statecleaned',"CO"))	
	replace	statecode = 	7 	if(regexm(`statecleaned',"CT"))	
	replace	statecode = 	8 	if(regexm(`statecleaned',"DE"))	
	replace	statecode = 	9 	if(regexm(`statecleaned',"FL"))	
	replace	statecode = 	10	if(regexm(`statecleaned',"GA"))	
	replace	statecode = 	11	if(regexm(`statecleaned',"HI"))	
	replace	statecode = 	12	if(regexm(`statecleaned',"ID"))	
	replace	statecode = 	13	if(regexm(`statecleaned',"IL"))
	replace	statecode = 	14	if(regexm(`statecleaned',"IN"))
	replace	statecode = 	15	if(regexm(`statecleaned',"IA"))   
	replace	statecode = 	16	if(regexm(`statecleaned',"KS"))
	replace	statecode = 	17	if(regexm(`statecleaned',"KY"))
	replace	statecode = 	18	if(regexm(`statecleaned',"LA"))
	replace	statecode = 	19	if(regexm(`statecleaned',"ME"))	
	replace	statecode = 	20	if(regexm(`statecleaned',"MD"))
	replace	statecode = 	21	if(regexm(`statecleaned',"MA"))
	replace	statecode = 	22	if(regexm(`statecleaned',"MI"))
	replace	statecode = 	23	if(regexm(`statecleaned',"MN"))
	replace	statecode = 	24	if(regexm(`statecleaned',"MS"))
	replace	statecode = 	25	if(regexm(`statecleaned',"MO"))
	replace	statecode = 	26	if(regexm(`statecleaned',"MT"))
	replace	statecode = 	27	if(regexm(`statecleaned',"NE"))
	replace	statecode = 	28	if(regexm(`statecleaned',"NV"))
	replace	statecode = 	29	if(regexm(`statecleaned',"NH"))
	replace	statecode = 	30	if(regexm(`statecleaned',"NJ"))
	replace	statecode = 	31	if(regexm(`statecleaned',"NM"))
	replace	statecode = 	32	if(regexm(`statecleaned',"NY"))
	replace	statecode = 	33	if(regexm(`statecleaned',"NC"))  
	replace	statecode = 	34	if(regexm(`statecleaned',"ND"))
	replace	statecode = 	35	if(regexm(`statecleaned',"OH"))   
	replace	statecode = 	36	if(regexm(`statecleaned',"OK"))
	replace	statecode = 	37	if(regexm(`statecleaned',"OR"))
	replace	statecode = 	38	if(regexm(`statecleaned',"PA"))
	replace	statecode = 	39	if(regexm(`statecleaned',"RI"))
	replace	statecode = 	40	if(regexm(`statecleaned',"SC"))  
	replace	statecode = 	41	if(regexm(`statecleaned',"SD"))
	replace	statecode = 	42	if(regexm(`statecleaned',"TN"))
	replace	statecode = 	43	if(regexm(`statecleaned',"TX"))   
	replace	statecode = 	44	if(regexm(`statecleaned',"UT"))   
	replace	statecode = 	45	if(regexm(`statecleaned',"VT"))	
	replace	statecode = 	46	if(regexm(`statecleaned',"VA"))	
	replace	statecode = 	47	if(regexm(`statecleaned',"WA"))	
	replace	statecode = 	48	if(regexm(`statecleaned',"WV"))	
	replace	statecode = 	49	if(regexm(`statecleaned',"WI"))	
	replace	statecode = 	50	if(regexm(`statecleaned',"WY"))	
	replace statecode =	51	if(regexm(`statecleaned',"DC"))
	replace statecode =	52	if(regexm(`statecleaned',"AS"))
	replace statecode =	53	if(regexm(`statecleaned',"GU"))
	replace statecode =	54	if(regexm(`statecleaned',"PR"))
	replace statecode =	55	if(regexm(`statecleaned',"VI"))
}
if("`2'" ~= "postal") {
	di "Not using postal abbreviations."
	replace	statecode = 	1 	if(	regexm(`statecleaned',"ALABAMA"))
	replace	statecode = 	2 	if(	regexm(`statecleaned',"ALASKA"))
	replace	statecode = 	3 	if(	regexm(`statecleaned',"ARIZONA"))
	replace	statecode = 	4 	if(	regexm(`statecleaned',"ARKANSAS"))
	replace	statecode = 	5 	if(	regexm(`statecleaned',"CALIFORNIA"))
	replace	statecode = 	6 	if(	regexm(`statecleaned',"COLORADO"))
	replace	statecode = 	7 	if(	regexm(`statecleaned',"CONNECTICUT"))
	replace	statecode = 	8 	if(	regexm(`statecleaned',"DELAWARE"))
	replace	statecode = 	9 	if(	regexm(`statecleaned',"FLORIDA"))
	replace	statecode = 	10	if(	regexm(`statecleaned',"GEORGIA"))
	replace	statecode = 	11	if(	regexm(`statecleaned',"HAWAII"))
	replace	statecode = 	12	if(	regexm(`statecleaned',"IDAHO"))
	replace	statecode = 	13	if(	regexm(`statecleaned',"ILLINOIS"))
	replace	statecode = 	14	if(	regexm(`statecleaned',"INDIANA"))
	replace	statecode = 	15	if(	regexm(`statecleaned',"IOWA"))
	replace	statecode = 	16	if(	regexm(`statecleaned',"KANSAS") 		& ~regexm(`statecleaned',"ARKANSAS"))
	replace	statecode = 	17	if(	regexm(`statecleaned',"KENTUCKY"))
	replace	statecode = 	18	if(	regexm(`statecleaned',"LOUISIANA"))
	replace	statecode = 	19	if(	regexm(`statecleaned',"MAINE"))
	replace	statecode = 	20	if(	regexm(`statecleaned',"MARYLAND"))
	replace	statecode = 	21	if(	regexm(`statecleaned',"MASSACHUSETTS"))
	replace	statecode = 	22	if(	regexm(`statecleaned',"MICHIGAN"))
	replace	statecode = 	23	if(	regexm(`statecleaned',"MINNESOTA"))
	replace	statecode = 	24	if(	regexm(`statecleaned',"MISSISSIPPI"))
	replace	statecode = 	25	if(	regexm(`statecleaned',"MISSOURI"))
	replace	statecode = 	26	if(	regexm(`statecleaned',"MONTANA"))
	replace	statecode = 	27	if(	regexm(`statecleaned',"NEBRASKA"))
	replace	statecode = 	28	if(	regexm(`statecleaned',"NEVADA"))
	replace	statecode = 	29	if(	regexm(`statecleaned',"NEWHAMPSHIRE"))
	replace	statecode = 	30	if(	regexm(`statecleaned',"NEWJERSEY"))
	replace	statecode = 	31	if(	regexm(`statecleaned',"NEWMEXICO"))
	replace	statecode = 	32	if(	regexm(`statecleaned',"NEWYORK"))
	replace	statecode = 	33	if(	regexm(`statecleaned',"NORTHCAROLINA"))
	replace	statecode = 	34	if(	regexm(`statecleaned',"NORTHDAKOTA"))
	replace	statecode = 	35	if(	regexm(`statecleaned',"OHIO"))
	replace	statecode = 	36	if(	regexm(`statecleaned',"OKLAHOMA"))
	replace	statecode = 	37	if(	regexm(`statecleaned',"OREGON"))
	replace	statecode = 	38	if(	regexm(`statecleaned',"PENNSYLVANIA"))
	replace	statecode = 	39	if(	regexm(`statecleaned',"RHODEISLAND"))
	replace	statecode = 	40	if(	regexm(`statecleaned',"SOUTHCAROLINA"))
	replace	statecode = 	41	if(	regexm(`statecleaned',"SOUTHDAKOTA"))
	replace	statecode = 	42	if(	regexm(`statecleaned',"TENNESSEE"))
	replace	statecode = 	43	if(	regexm(`statecleaned',"TEXAS"))
	replace	statecode = 	44	if(	regexm(`statecleaned',"UTAH"))
	replace	statecode = 	45	if(	regexm(`statecleaned',"VERMONT"))
	replace	statecode = 	46	if(	regexm(`statecleaned',"VIRGINIA"))
	replace	statecode = 	47	if(	regexm(`statecleaned',"WASHINGTON") 		& ~regexm(`statecleaned',"DC"))
	replace	statecode = 	48	if(	regexm(`statecleaned',"WESTVIRGINIA"))
	replace	statecode = 	49	if(	regexm(`statecleaned',"WISCONSIN"))
	replace	statecode = 	50	if(	regexm(`statecleaned',"WYOMING"))
	replace statecode =	51	if(	regexm(`statecleaned',"DC") 			|  regexm(`statecleaned',"DISTRICTOFCOLUMBIA")) 
	replace statecode =	52	if(	regexm(`statecleaned',"AMERICANSAMOA"))
	replace statecode =	53	if(	regexm(`statecleaned',"GUAM"))
	replace statecode =	54	if(	regexm(`statecleaned',"PUERTORICO"))
	replace statecode =	55	if(	regexm(`statecleaned',"VIRGINISLAND"))
}

di "Unmatched values:"
tab `1' if statecode == .

capture label drop statelabel
label define statelabel		///
1  "Alabama"              	///
2  "Alaska"               	///
3  "Arizona"              	///
4  "Arkansas"             	///
5  "California"           	///
6  "Colorado"             	///
7  "Connecticut"          	///
8  "Delaware"             	///
9  "Florida"              	///
10 "Georgia"              	///
11 "Hawaii"               	///
12 "Idaho"                	///
13 "Illinois"             	///
14 "Indiana"              	///
15 "Iowa"                 	///
16 "Kansas"               	///
17 "Kentucky"             	///
18 "Louisiana"            	///
19 "Maine"                	///
20 "Maryland"             	///
21 "Massachusetts"        	///
22 "Michigan"             	///
23 "Minnesota"            	///
24 "Mississippi"          	///
25 "Missouri"             	///
26 "Montana"              	///
27 "Nebraska"             	///
28 "Nevada"               	///
29 "New Hampshire"        	///
30 "New Jersey"           	///
31 "New Mexico"           	///
32 "New York"             	///
33 "North Carolina"       	///
34 "North Dakota"         	///
35 "Ohio"                 	///
36 "Oklahoma"             	///
37 "Oregon"               	///
38 "Pennsylvania"         	///
39 "Rhode Island"         	///
40 "South Carolina"       	///
41 "South Dakota"         	///
42 "Tennessee"            	///
43 "Texas"                	///
44 "Utah"                 	///
45 "Vermont"              	///
46 "Virginia"             	///
47 "Washington"           	///
48 "West Virginia"        	///
49 "Wisconsin"            	///
50 "Wyoming"              	///
51 "District of Columbia" 	///
52 "American Samoa"		///
53 "Guam"			///
54 "Puerto Rico"		///
55 "Virgin Islands"
label values statecode statelabel

end
